home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / 92052tar.gz / 920528.tar / services.c < prev    next >
C/C++ Source or Header  |  1992-05-26  |  4KB  |  151 lines

  1. /* @(#) $Header: services.c,v 1.4 92/05/26 10:09:09 deyke Exp $ */
  2.  
  3. #include <ctype.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #include "global.h"
  8. #include "socket.h"
  9. #include "netuser.h"
  10.  
  11. struct port_table {
  12.   char  *name;
  13.   int  port;
  14. };
  15.  
  16. static struct port_table tcp_port_table[] = {
  17.   "*",           0,
  18.   "convers",     3600,
  19.   "discard",     IPPORT_DISCARD,/* ARPA discard protocol */
  20.   "domain",      IPPORT_DOMAIN, /* ARPA domain nameserver */
  21.   "echo",        IPPORT_ECHO,   /* ARPA echo protocol */
  22.   "finger",      IPPORT_FINGER, /* ARPA finger protocol */
  23.   "ftp",         IPPORT_FTP,    /* ARPA file transfer protocol (cmd) */
  24.   "ftp-data",    IPPORT_FTPD,   /* ARPA file transfer protocol (data) */
  25.   "netupds",     4715,
  26.   "nntp",        IPPORT_NNTP,
  27.   "pop2",        IPPORT_POP,    /* Post Office Prot. v2 */
  28.   "pop3",        110,           /* Post Office Prot. v3 */
  29.   "smtp",        IPPORT_SMTP,   /* ARPA simple mail transfer protocol */
  30.   "telnet",      IPPORT_TELNET, /* ARPA virtual terminal protocol */
  31.   "ttylink",     IPPORT_TTYLINK,
  32.   NULLCHAR
  33. };
  34.  
  35. static struct port_table udp_port_table[] = {
  36.   "*",           0,
  37.   "bootpc",      IPPORT_BOOTPC,
  38.   "bootps",      IPPORT_BOOTPS,
  39.   "domain",      IPPORT_DOMAIN, /* ARPA domain nameserver */
  40.   "remote",      IPPORT_REMOTE,
  41.   "rip",         IPPORT_RIP,
  42.   NULLCHAR
  43. };
  44.  
  45. static char *nextstr __ARGS((void));
  46. static char *port_name __ARGS((struct port_table *table, int port));
  47. static int port_number __ARGS((struct port_table *table, char *name));
  48.  
  49. /*---------------------------------------------------------------------------*/
  50.  
  51. static char  *nextstr()
  52. {
  53.  
  54. #define NUMSTR  16
  55.  
  56.   static char  strstore[NUMSTR][128];
  57.   static int  strindex;
  58.  
  59.   strindex++;
  60.   if (strindex >= NUMSTR) strindex = 0;
  61.   return strstore[strindex];
  62. }
  63.  
  64. /*---------------------------------------------------------------------------*/
  65.  
  66. static char  *port_name(table, port)
  67. struct port_table *table;
  68. int  port;
  69. {
  70.   char  *p;
  71.  
  72.   for (; table->name; table++)
  73.     if (port == table->port) return table->name;
  74.   p = nextstr();
  75.   sprintf(p, "%u", port);
  76.   return p;
  77. }
  78.  
  79. /*---------------------------------------------------------------------------*/
  80.  
  81. char  *tcp_port_name(port)
  82. int  port;
  83. {
  84.   return port_name(tcp_port_table, port);
  85. }
  86.  
  87. /*---------------------------------------------------------------------------*/
  88.  
  89. char  *udp_port_name(port)
  90. int  port;
  91. {
  92.   return port_name(udp_port_table, port);
  93. }
  94.  
  95. /*---------------------------------------------------------------------------*/
  96.  
  97. static int  port_number(table, name)
  98. struct port_table *table;
  99. char  *name;
  100. {
  101.   int  len;
  102.  
  103.   if (!isdigit(uchar(*name))) {
  104.     len = strlen(name);
  105.     for (; table->name; table++)
  106.       if (!strncmp(name, table->name, len)) return table->port;
  107.   }
  108.   return atoi(name);
  109. }
  110.  
  111. /*---------------------------------------------------------------------------*/
  112.  
  113. int  tcp_port_number(name)
  114. char  *name;
  115. {
  116.   return port_number(tcp_port_table, name);
  117. }
  118.  
  119. /*---------------------------------------------------------------------------*/
  120.  
  121. int  udp_port_number(name)
  122. char  *name;
  123. {
  124.   return port_number(udp_port_table, name);
  125. }
  126.  
  127. /*---------------------------------------------------------------------------*/
  128.  
  129. char  *pinet_tcp(s)
  130. struct socket *s;
  131. {
  132.   char  *p;
  133.  
  134.   p = nextstr();
  135.   sprintf(p, "%s:%s", inet_ntoa(s->address), tcp_port_name(s->port));
  136.   return p;
  137. }
  138.  
  139. /*---------------------------------------------------------------------------*/
  140.  
  141. char  *pinet_udp(s)
  142. struct socket *s;
  143. {
  144.   char  *p;
  145.  
  146.   p = nextstr();
  147.   sprintf(p, "%s:%s", inet_ntoa(s->address), udp_port_name(s->port));
  148.   return p;
  149. }
  150.  
  151.